package com.wenlin.myproduction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.BuildConfig;
import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseActivity;
import com.wenlin.myproduction.base.BaseSwipeActivity;
import com.wenlin.myproduction.bean.GuessNumberBean;
import com.wenlin.myproduction.binder.GuessNumberBinder;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.LogUtils;
import com.wenlin.myproduction.util.MyGridLayoutManager;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.UI;
import com.wenlin.myproduction.util.ValCommon;

import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class GuessNumberActivity extends BaseSwipeActivity {
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_try)
    TextView tvTry;
    @BindView(R.id.rv)
    RecyclerView rv;

    private Items items;
    private MultiTypeAdapter adapter;
    private StringBuilder sb;
    private Random random;
    private String target;

    public static void start(BaseActivity activity) {
        Intent intent = new Intent(activity, GuessNumberActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_guess_number;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(ValCommon.GUESS_NUMBER_GAME);
        items = new Items();
        adapter = new MultiTypeAdapter(items);
        adapter.register(GuessNumberBean.class, new GuessNumberBinder());
        rv.setLayoutManager(new MyGridLayoutManager(this, 1));
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
    }

    /**
     * 设置题目
     */
    public void resetTitle() {
        sb = new StringBuilder();
        if (Checker.isNull(random)) random = new Random();
        while (sb.toString().length() < 4) {
            int i = random.nextInt(10);
            if (!sb.toString().contains(String.valueOf(i)))
                sb.append(i);
        }
        target = sb.toString();
        LogUtils.e("题目是" + target);
        if (BuildConfig.DEBUG) ToastUtils.getInstance().showInfo("连线调试时才显示：谜底是" + target);
        UI.setVisibility(et, View.VISIBLE);
        UI.setVisibility(tvTry, View.VISIBLE);
        UI.setVisibility(tvStart, View.INVISIBLE);
    }

    /**
     * 检查结果
     *
     * @param answer 提交的答案
     */
    private void checkAnswer(String answer) {
        int totalHas = 0;
        int totalCorrect = 0;
        for (int i = 0; i < answer.length(); i++) {
            String c = answer.substring(i, i + 1);
            if (target.contains(c)) {
                totalHas++;
            }
            if (target.substring(i, i + 1).equals(c)) {
                totalCorrect++;
                totalHas--;//数字对，且位置也对，表示为A，此时totalHas需要-1；
            }
        }
        items.add(0, new GuessNumberBean(items.size(), answer, totalHas, totalCorrect));
        adapter.notifyDataSetChanged();
        if (totalCorrect == 4) {
            ToastUtils.getInstance().showSuccess("恭喜你，猜对了");
            UI.setVisibility(et, View.INVISIBLE);
            UI.setVisibility(tvTry, View.INVISIBLE);
            UI.setVisibility(tvStart, View.VISIBLE);
        } else {
            UI.setText(et, "");
            UI.setSelection(et, 0);
        }
    }

    @OnClick({R.id.tv_start, R.id.tv_try})
    void click(View view) {
        int id = view.getId();
        if (id == R.id.tv_start) {
            resetTitle();
            ToastUtils.getInstance().showInfo(R.string.title_already_now_please_try);
        } else if (id == R.id.tv_try) {
            String etStr = UI.getEtText(et);
            if (Checker.hasNoWord(etStr) || etStr.length() != 4) {
                ToastUtils.getInstance().showError("答案应为四个数字");
                return;
            }
            checkAnswer(etStr);
        }
    }
}
